package org.subject.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.subject.dao.MessageMapper;
import org.subject.dto.MessageAndUser;
import org.subject.entity.Message;
import org.subject.entity.Page;
import org.subject.entity.User;
import org.subject.service.MessageService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 竞赛信息实现类
 * Created by kai on 2017/4/24.
 */
@Service
public class MessageServiceImpl implements MessageService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    // 注入Service依赖
    @Autowired
    private MessageMapper messageMapper;

    public List<Message> getMessageList() {
        return null;
    }

    public Message getById(long messageId) {
        return messageMapper.selectByPrimaryKey(messageId);
    }

    public boolean insertMessage(Message message) {
        return false;
    }

    public List<MessageAndUser> queryAll() {
        return messageMapper.queryAll();
    }

    /**
     * 搜索和分页查询数据
     *
     * @param userName
     * @param title
     * @param currentPage
     * @return
     */
    public List<MessageAndUser> queryMessageList(String userName, String title, int currentPage) {
        //组织消息对象
        Map<String, Object> countParamter = new HashMap<String, Object>();
        Message message = new Message();
        message.setMessageTitle(title);
        User user = new User();
        user.setUserName(userName);
        countParamter.put("message", message);
        countParamter.put("user", user);
        Page page = new Page();
        page.setCurrentPage(currentPage);
        //根据条件查询条数
        int totalNumber = messageMapper.queryMessageCount(countParamter);
        //组织分页查询参数
        page.setTotalNumber(totalNumber);
        //针对 MyBatis 只能传一个参数，故封装成一个 Map 对象
        Map<String, Object> listParamter = new HashMap<String, Object>();
        listParamter.put("message", message);
        listParamter.put("page", page);
        //分页查询并返回结果
        return messageMapper.queryMessageList(listParamter);
    }

    /**
     * 获取消息列表的条数
     *
     * @param userName
     * @param title
     * @return
     */
    public int queryMessageCount(String userName, String title) {
        //组织消息对象
        Map<String, Object> countParamter = new HashMap<String, Object>();
        Message message = new Message();
        message.setMessageTitle(title);
        User user = new User();
        user.setUserName(userName);
        countParamter.put("message", message);
        countParamter.put("user", user);
        //根据条件查询条数
        return messageMapper.queryMessageCount(countParamter);
    }

    public List<Message> queryAllMessage() {
        return null;
    }

    /**
     * 通过竞赛信息id获取竞赛详情信息
     *
     * @param messageId
     * @return
     */
    public MessageAndUser selectMUByKey(long messageId) {
        return messageMapper.selectMUByKey(messageId);
    }

    /**
     * 创建竞赛
     *
     * @param message
     * @return
     */
    public int insert(Message message) {
        return messageMapper.insertSelective(message);
    }

    /**
     * 根据竞赛id删除竞赛信息
     *
     * @param messageId
     * @return
     */
    public int deleteByPrimaryKey(Long messageId) {
        return messageMapper.deleteByPrimaryKey(messageId);
    }

    /**
     * 根据发布者id查询数据
     *
     * @param userId
     * @param currentPage
     * @return
     */
    public List<MessageAndUser> queryMessageByUId(String userId, int currentPage) {
        //组织消息对象
        Map<String, Object> countParamter = new HashMap<String, Object>();
        User user = new User();
        user.setUserId(Long.valueOf(userId));
        countParamter.put("user", user);
        Page page = new Page();
        page.setCurrentPage(currentPage);
        //根据条件查询条数
        int totalNumber = messageMapper.queryCountByUId(countParamter);
        //组织分页查询参数
        page.setTotalNumber(totalNumber);
        //针对 MyBatis 只能传一个参数，故封装成一个 Map 对象
        Map<String, Object> listParamter = new HashMap<String, Object>();
        listParamter.put("user", user);
        listParamter.put("page", page);
        //分页查询并返回结果
        return messageMapper.queryMessageByUId(listParamter);
    }

    /**
     * 根据发布者id获取消息列表的条数
     *
     * @param userId
     * @return
     */
    public int queryCountByUId(String userId) {
        //组织消息对象
        Map<String, Object> countParamter = new HashMap<String, Object>();
        User user = new User();
        user.setUserId(Long.valueOf(userId));
        countParamter.put("user", user);
        //根据条件查询条数
        return messageMapper.queryCountByUId(countParamter);
    }

    public List<MessageAndUser> queryMessageByFId(String facultyId, int currentPage) {
        Map<String, Object> paramter = new HashMap<String, Object>();
        User user = new User();
        logger.info("-----------------------------" + facultyId);
        if (facultyId != null) {
            user.setFacultyId(Long.valueOf(facultyId));
        }
        paramter.put("user", user);
        Page page = new Page();
        page.setCurrentPage(currentPage);
        //根据条件查询条数
        int totalNumber = messageMapper.queryCountByFId(paramter);
        //组织分页查询参数
        page.setTotalNumber(totalNumber);
        //针对 MyBatis 只能传一个参数，故封装成一个 Map 对象
        Map<String, Object> listParamter = new HashMap<String, Object>();
        listParamter.put("user", user);
        listParamter.put("page", page);
        return messageMapper.queryMessageByFId(listParamter);
    }

    public int queryCountByFId(String facultyId) {
        //组织消息对象
        Map<String, Object> countParamter = new HashMap<String, Object>();
        User user = new User();
        if (facultyId != null) {
            user.setUserId(Long.valueOf(facultyId));
        }
        countParamter.put("user", user);
        return messageMapper.queryCountByFId(countParamter);
    }
}
